package ljl.alg.jianzhioffer.round3.hard;

import commons.TreeNode;

import java.util.Arrays;
import java.util.LinkedList;

public class _37_codec {

    class Codec {

        public String serialize(TreeNode root) {
            if (root == null) return "o,";
            return root.val + "," + serialize(root.left) + serialize(root.right);
        }

        public TreeNode deserialize(String data) {
            String[] nums = data.split(",");
            return dec(new LinkedList<>(Arrays.asList(nums)));
        }

        TreeNode dec(LinkedList<String> nums) {
            String s = nums.pollFirst();
            if (s.equals("o")) return null;
            TreeNode root = new TreeNode(Integer.parseInt(s));
            root.left = dec(nums);
            root.right = dec(nums);
            return root;
        }

    }
}
